import { tabsData } from '../flow-data';
import {
  getNewFlowPageApi,
  getFlowPageApi,
  getHighlightWorkItemApi,
  getViewApprovalProcessApi,
  getFlowXmlApi,
} from '/@/api/flow/flow';
import { isObjectEmpty, isArray } from '/@/utils/is';
import editorStore from '/@/store/modules/bpmnEditor';
const editor = editorStore();

export async function useInitFlow(data, proxyData) {
  const { flowDataRef, flowCheckParams, flowProcessDesc, flowStore } = proxyData;

  if (!data) {
    return { flowDataRef, flowCheckParams, flowProcessDesc };
  }

  // 获取流程图
  async function getFlowXml(guid) {
    const ret = await getFlowXmlApi(guid);
    return ret;
  }

  const { moduleId, isCheck: isCheckTemp, record, templateId, fieldsMap } = data;
  flowProcessDesc.isCheck = isCheckTemp;
  flowDataRef.moduleId = moduleId;

  let flowDetail;

  if (record) {
    // 列表行按钮打开
    const { WorkItemID, EntryID, OperationType } = record;

    flowProcessDesc.flowDataArr = tabsData;
    flowCheckParams.value = { workItemID: WorkItemID, entryID: EntryID };

    // 传阅类型 0传阅；1征询
    let operationType = OperationType;

    if (operationType && typeof operationType == 'object') {
      operationType = operationType.value;
    }

    const params = {
      modelShowType: flowProcessDesc.isCheck ? 2 : '',
      moduleId,
      workItemID: WorkItemID,
      entryID: EntryID,
      extendOpenParam: {
        operationType,
      },
    };

    flowDetail = await getFlowPageApi(params);

    const ret = await getFlowXml(flowDetail.guid);

    flowDataRef.processXml = ret.bpmnXml;

    editor.updateTheme(ret.theme ? Number(ret.theme) : 2);

    const highlightNode = await getHighlightWorkItemApi(WorkItemID);

    flowDataRef.finishedInfo = highlightNode;

    const processDesc = await getViewApprovalProcessApi(WorkItemID);
    flowProcessDesc.processDesc = processDesc.list;
  } else {
    //新建流程
    flowProcessDesc.flowDataArr = flowProcessDesc.flowDataArr.filter((_, index) => index <= 1);

    flowDetail = await getNewFlowPageApi({
      cyclostyleID: templateId,
      moduleId: Number(moduleId),
      // 扩展业务参数
      fieldsMap: isArray(fieldsMap) ? fieldsMap[0] : fieldsMap,
    });

    const { workItemID, entryId } = flowDetail.workItemDto;
    flowCheckParams.value = { workItemID: workItemID, entryID: entryId };
    const ret = await getFlowXml(flowDetail.guid);
    flowDataRef.processXml = ret.bpmnXml;
    editor.updateTheme(ret.theme ? Number(ret.theme) : 2);
  }

  flowDataRef.workItemName = flowDetail.workItemDto.workItemName;
  flowDataRef.workItemDto = flowDetail.workItemDto;

  flowProcessDesc.isManage =
    flowCheckParams.value.workItemID != -1 &&
    !!flowDataRef.workItemDto.formMap.resultMap &&
    !isObjectEmpty(flowDataRef.workItemDto.formMap.resultMap);

  flowDataRef.guid = flowDetail.guid;
  flowDataRef.dataModelId = flowDataRef.workItemDto.formMap.modelId;

  flowStore.setFlowReleaseParams({ ...flowCheckParams.value, guid: flowDataRef.guid });
  console.log(
    '%cflowDataRef===>92： ',
    'background: rgb(23, 197, 237,.6); color: #ff5023; font-size:18px;font-weight:700',
    flowDataRef,
  );

  return { flowDataRef, flowCheckParams, flowProcessDesc };
}
